
/**
 * 业务平台基类
 */
var sps = sps || {};

/**
 * 全局引用，大部分情况下为'window'
 */
sps.global = this;

/**
 * JS基本目录
 */
sps.baseJsPath = BASE_JS_PATH || './';

/**
 * CSS基本目录
 */
sps.baseCssPath = BASE_THEME_PATH || 'theme/default/';

/**
 * 空函数，什么事都不做,可用于默认回调函数等
 * 
 * @return {void} 空
 */
sps.fGo = function() {
};

/**
 * 命名空间定义
 */
sps.provide = function(name) {
	if (sps.isProvided_(name)) {
		return;
	}

	sps.exportPath_(name);
};

/**
 * 解决动态依赖,必须在deps.js中设置动态依赖关系
 */
sps.require = function(name) {
	if (sps.included_[name]) {
		return;
	}

	var r = deps[name];
	if (r) {
		if (r.deps) {
			r.deps.forEach(function(d) {
				sps.require(d);
			});
		}

		var src = r.src;
		sps.included_[name] = true;
		sps.writeScripts_(src);
	}
};

/**
 * CSS依赖
 */
sps.requireCss = function(css) {
    var doc = sps.global.document;

    var csss = doc.getElementsByTagName("link");
    for (var k = 0, len = csss.length; k < len; k++) {
        if (csss[k].href.indexOf(css) != -1) {
            return;
        }
    }

    var cssDoc = doc.createElement('link');
    cssDoc.setAttribute('type', 'text/css');
    cssDoc.setAttribute('rel', 'stylesheet');

    var href = sps.baseCssPath + css;
    cssDoc.setAttribute('href', href);

    var toDoc = doc.getElementsByTagName("head").length ? doc.getElementsByTagName("head")[0] : doc.body;
    toDoc.appendChild(cssDoc);
};

/**
 * 加载脚本
 */
sps.include = function(src) {
    var doc = sps.global.document;
    doc.write('<script type="text/javascript" src="' + src + '"></' + 'script>');
};

/**
 * 加载依赖文件库
 */
sps.include(sps.baseJsPath + 'deps.js');

/**
 * Inherit the prototype methods from one constructor into another. Usage:
 * 
 * <pre>
 * function ParentClass(a, b) {
 * }
 * ParentClass.prototype.foo = function(a) {
 * }
 * 
 * function ChildClass(a, b, c) {
 * 	ParentClass.call(this, a, b);
 * }
 * 
 * goog.inherits(ChildClass, ParentClass);
 * 
 * var child = new ChildClass('a', 'b', 'see');
 * child.foo(); // works
 * </pre>
 * 
 * In addition, a superclass' implementation of a method can be invoked as
 * follows:
 * 
 * <pre>
 * ChildClass.prototype.foo = function(a) {
 * 	ChildClass.superClass_.foo.call(this, a);
 * 	// other code
 * 	};
 * </pre>
 * 
 * @param {Function} childCtor Child class.
 * @param {Function} parentCtor Parent class.
 */
sps.inherits = function(childCtor, parentCtor) {
	/** @constructor */
	function tempCtor() {
	}

	tempCtor.prototype = parentCtor.prototype;
	childCtor.superClass_ = parentCtor.prototype;
	childCtor.prototype = new tempCtor();
	childCtor.prototype.constructor = childCtor;
};

/**
 * id指针
 */
sps.id_ = 1;
/**
 * 获取下一个Id号
 * 
 * @param {String} [prefix] Id前缀
 * @returns {String} 下一个Id号
 */
sps.$Id = function(prefix) {
	if (sps.lang.is(prefix, 'string'))
		return prefix + sps.id_++;
	else
		return 'appb' + sps.id_++;
};

/**
 * 创建唯一键
 */
sps.createGUID = function() {
	var d1 = new Date();
	var i = ('' + d1.getTime()).substring(6);
	var s = i + '' + Math.floor(Math.random() * 2925) + '' + Math.floor(Math.random() * 991);
	s += Math.floor(Math.random() * 9999);

	return s;
};

/**
 * 记录所有加载的js
 */
sps.included_ = {};

/**
 * 记录所有的命名空间
 */
sps.implicitNamespaces_ = {};

sps.exportPath_ = function(name) {
	var parts = name.split('.');
	var cur = sps.global;
	var ns = '';

	if (!(parts[0] in cur) && cur.execScript) {
		cur.execScript('var ' + parts[0]);
	}

	for ( var part; parts.length && (part = parts.shift());) {
		ns = ns ? ns + '.' + part : part;
		if (cur[part]) {
			cur = cur[part];
		} else {
			sps.implicitNamespaces_[ns] = true;
			cur = cur[part] = {};
		}
	}
};

/**
 * 命名空间是否已经定义
 */
sps.isProvided_ = function(name) {
	return sps.implicitNamespaces_[name];
};

sps.writeScripts_ = function(src) {
	sps.writeScriptTag_(sps.baseJsPath + src);
};

sps.writeScriptTag_ = function(src) {
	if (sps.inHtmlDocument_()) {
		sps.include(src);
		return true;
	} else {
		return false;
	}
};

sps.inHtmlDocument_ = function() {
	var doc = sps.global.document;
	return typeof doc != 'undefined' && 'write' in doc;
};

/**
 * 显示模式窗体
 */
sps.showModelWindow =
		function(url, args, width, height) {
			var top = (window.screen.availHeight - 30 - height) / 2;
			var left = (window.screen.availWidth - 10 - width) / 2;
			window.open(url, args, 'height=' + height + ',innerHeight=' + height + ',width=' + width + ',innerWidth='
					+ width + ',top=' + top + ',left=' + left
					+ ',toolbar=no,menubar=none,scrollbars=auto,resizeable=no,location=no,status=no');
		};

/**
 * 文件下载
 * 
 * @param url 文件路径
 */
sps.downloadFile = function(url) {
	if (typeof (sps.downloadFile.iframe) == "undefined") {
		var iframe = document.createElement("iframe");
		sps.downloadFile.iframe = iframe;
		document.body.appendChild(sps.downloadFile.iframe);
	}

	sps.downloadFile.iframe.src = url;
	sps.downloadFile.iframe.style.display = "none";
};

/**
 * 加载所有必须的库
 */
sps.include(sps.baseJsPath + 'core/constants.js');
sps.include(sps.baseJsPath + 'core/env.js');
sps.include(sps.baseJsPath + 'core/html5.js');
sps.include(sps.baseJsPath + 'core/json.js');
sps.include(sps.baseJsPath + 'core/lang.js');
sps.include(sps.baseJsPath + 'dom/dom.js');
sps.include(sps.baseJsPath + 'core/event.js');
sps.include(sps.baseJsPath + 'core/eventobject.js');
sps.include(sps.baseJsPath + 'core/eventsource.js');
